using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.Rules.Properties;
using Common.Utilities.DLR;

namespace Workflows.Components.Rules
{
    /// <summary>
    /// removed ORM
    /// </summary>
    [Serializable]
    public class RuleComponent:WorkflowComponentBase
    {
        #region props
        private RuleComponentInstruction _Instruction;
        private RuleComponentOutput _Output;

        /// <summary>
        /// 
        /// </summary>
        public override InstructionBase Instruction
        {
            get { return _Instruction; }
            set { _Instruction =(RuleComponentInstruction) value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public override ResultBase Output
        {
            get { return _Output; }
            set { _Output = (RuleComponentOutput) value; }
        }

        /// <summary></summary>
        public string EvaluableAssemblyFilePath
        {
            get
            {
                return this._Instruction.EvaluableAssemblyFilePath;
            }
            set
            {
                this._Instruction.EvaluableAssemblyFilePath = value;
            }
        }

        /// <summary></summary>
        public string EvaluableTypeName
        {
            get
            {
                return this._Instruction.EvaluableTypeName;
            }
            set
            {
                this._Instruction.EvaluableTypeName = value;
            }
        }

        /// <summary></summary>
        public Guid RuleID
        {
            get
            {
                return this._Instruction.RuleID;
            }
            set
            {
                this._Instruction.RuleID = value;
            }
        }
        #endregion

        ///<summary>
        ///</summary>
        public RuleComponent():base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.Rules.ToString();
            this.ComponentIcon = Resources.certmgr_445;
            this.ComponentImage = Resources.ApproveForm;
            this.ComponentName = "Rule";
            this.Description = "Evaluate conditions";

            this._Instruction = new RuleComponentInstruction(this.ComponentID, Guid.Empty, Guid.Empty, string.Empty, string.Empty);
            this._Output = new RuleComponentOutput(this.ComponentID, Guid.Empty, null);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "RuleID", this.RuleID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "EvaluableAssemblyFilePath", this.EvaluableAssemblyFilePath);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "EvaluableTypeName", this.EvaluableTypeName);
            
            return xNode;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xNode"></param>
        /// <returns></returns>
        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                RuleComponent component = base.Instantiate(xNode) as RuleComponent;

                component.RuleID = new Guid(xNode.Attributes.GetNamedItem("RuleID").Value);
                component.EvaluableAssemblyFilePath = xNode.Attributes.GetNamedItem("EvaluableAssemblyFilePath").Value;
                component.EvaluableTypeName = xNode.Attributes.GetNamedItem("EvaluableTypeName").Value;

                return component;
            }
            else
            {
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
            }
        }
    }
}
